<template>
  <el-container class="container">
    <div class="divx"></div>
    <div
      class="setting"
      :style="`${is_setting ? 'width: 100%;' : 'width: 5%;'}`"
    >
      <el-input
        v-model="input"
        placeholder="请输入URL"
        clearable
        size="mini"
        @keyup.enter.native="onSo"
      ></el-input>
    </div>
    <webview
      v-if="url"
      class="ifr"
      allowpopups
      :src="url"
      :plugins="true"
      useragent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
    ></webview>
  </el-container>
</template>

<script>
import hotkeys from "hotkeys-js";
import { ipcRenderer } from "electron";

export default {
  name: "web",
  data() {
    return {
      url: null,
      is_setting: true,
      input: "https://gamer.qq.com/v2/cloudgame/game/95179",
      // input: "https://start.qq.com/game/dnf/pc/?ADTAG=web-dnf-pc-guanwang#/",
    };
  },
  created() {
    this.onLoad();
    this.onKey();
  },
  methods: {
    onLoad() {},
    onSo() {
      this.url = this.input;
      ipcRenderer.send("webOpacity", "change");
      this.$nextTick(() => {
        const webview = document.querySelector("webview");

        webview.addEventListener("dom-ready", (e) => {
          var scriptString = `
            window.costomOpen = window.open;
            window.firstOpen = true;
            window.open = (url, name, specs, replace) => {
              var newUrl = window.location.href.replace(window.location.hash, '') + url
              navigator.clipboard.writeText(newUrl);
              window.costomOpen(url);
            }
          `;
          
          // window.costomOpen(url, name, specs, replace);
          //     setTimeout(() => {
          //       if (window.firstOpen) {
          //         window.location.href = newUrl;
          //         window.location.reload();
          //         window.firstOpen = false;
          //       }
          //     }, 10000);
          webview.executeJavaScript(scriptString);
        });

      });
    },
    open() {
      if (this.is_setting) {
        this.is_setting = false;
      } else {
        this.is_setting = true;
      }
    },
    onKey() {
      var that = this;

      hotkeys.filter = function (event) {
        return true;
      };

      hotkeys("*", function (e) {
        if (e.key === "z") {
          var t = document.querySelector(".ifr");
          t.reload();
        } 
        // else if (e.key === "-") {
        //   ipcRenderer.send("webOpacity", "-");
        // } else if (e.key === "+") {
        //   ipcRenderer.send("webOpacity", "+");
        // } 
        else if (e.key === "c") {
          that.open();
        } 
        // else if (e.key === "x") {
        //   ipcRenderer.send("webOpacity", "exit");
        // }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.container {
  width: 100%;
  height: 100%;
  border: 0px;

  .divx {
    width: 50px;
    height: 50px;
    position: absolute;
    z-index: 999;
    -webkit-app-region: drag;
  }

  .ifr {
    width: 1080px;
    height: 609px;
    border: 0px;
  }

  .setting {
    clear: both;
    position: absolute;
    padding-top: 0px;
    right: 0;
  }
}
</style>
